package view.panel.task
{
	import control.panel.IPanelControl;
	import control.panel.TaskPanelControl;
	
	import events.TabEvent;
	
	import flash.display.Shape;
	import flash.events.MouseEvent;
	
	import global.GameState;
	import global.Resource;
	
	import utils.DisplayTool;
	
	import view.components.ImgButton;
	import view.components.SimpleLoading;
	import view.components.UIComponents;
	import view.components.tab.Tab;
	import view.components.tab.TabBar;
	import view.panel.AbsPanel;
	
	public class TaskPanel extends AbsPanel
	{
		private var tab:Tab;
		private var dg:*;
		private var dg2:*;
		private var columns :Array;
		private var isUpdated:Boolean;
		private var simpleLoading:SimpleLoading;
		private var buts:Array;
		private var dropTaskBut:ImgButton;
		private var shape:Shape;
		
		public function TaskPanel(panelControl:IPanelControl)
		{
			super(panelControl);
			
			resetBg(624,305,.5,true);
			initCloseBut();
			resetCloseBut();
			initTitle("text_title_task");
			
			initTweenPanelOpen();
			
			initTab();
			initDataGrid();
			
			dropShadow();
			initLoading();
			initBut();
			//drawLine();
			
			
		}
		
		private function initBut():void
		{
			dropTaskBut = new ImgButton(270,22,0,onDropBut,300,24,"butBg2",null,"先从列表点选已接任务，然后点击这儿放弃任务");
			addChild(dropTaskBut);
		}
		
		private function onDropBut(event:MouseEvent):void
		{
			if (dg.selectedIndex >= 0)
			{
				var taskId:String = dg.getItemAt(dg.selectedIndex).id;
				(panelControl as TaskPanelControl).onTaskDrop(taskId);
			}
		}
		
		public function switchToGame():void
		{
			this.defaultX = 8;
			this.defaultY = 172
			DisplayTool.removeChild(this,dropTaskBut);
			this.shape.graphics.clear();
			resetBg(400,230,.5);
			resetCloseBut();
			drawFrame(dg2);
			drawLine2([77,135,85]);
			
			DisplayTool.removeChild(dg.parent,dg);
			addChild(dg2);
			
		}
		
		public function switchToHall():void
		{
			this.shape.graphics.clear();
			addChild(dropTaskBut);
			resetBg(624,305,1,true);
			resetCloseBut();
			
			drawLine();
			drawFrame(dg);
			DisplayTool.removeChild(dg2.parent,dg2);
			addChild(dg);
			
		}
		
		private function initLoading():void
		{
			simpleLoading = new SimpleLoading();
		}
		
		public function showLoading():void
		{
			dg.removeAll();
			simpleLoading.x = (dg.width - simpleLoading.width) * .5 + simpleLoading.width * .5 + dg.x;
			simpleLoading.y = (dg.height - simpleLoading.height) * .5 + simpleLoading.height* .5 + dg.y;
			simpleLoading.start();
			addChild(simpleLoading);
			this.dg.graphics.clear();
		}
		
		public function hideLoading():void
		{
			simpleLoading.stop();
			DisplayTool.removeChild(this,simpleLoading);
			if (!GameState.isInGameRoom)
			{
				drawLine();
			}
		}
		
		private function initDataGrid():void
		{
			
			buts = [];
			
			columns = ["name","desc","bonuse","state","progress"];
			
			dg = UIComponents.getDataGrid();
			dg.centers = [false,false,true,true,true];
			dg.setSize(580,240);
			dg.columns = columns;
			dg.rowHeight = 70;
			dg.x = 20;
			dg.y = 49;
			dg.getColumnAt(0).headerText = "任务名称";
			dg.getColumnAt(1).headerText = "任务描述";
			dg.getColumnAt(2).headerText = "任务奖励";
			dg.getColumnAt(3).headerText = "任务状态";
			dg.getColumnAt(4).headerText = "进度";
			
			
			dg.getColumnAt(0).width = 80;
			dg.getColumnAt(1).width = 280;
			dg.getColumnAt(2).width = 90;
			dg.getColumnAt(3).width = 80;
			dg.getColumnAt(4).width = 50;
			
			dg.setRendererStyle("skin",new Shape());
			dg.resizableColumns = false;
			
			dg.setStyle("cellRenderer",Resource.uicomponent.TaskCellClass);
			dg.iconIndex = 3;
			addChild(dg);
			
			
			shape = new Shape();
			addChild(shape);
			
			
			dg2 = UIComponents.getDataGrid();
			dg2.centers = [false,false,true,true];
			dg2.columns = ["name","desc","bonuse","progress"];
			dg2.x = 20;
			dg2.y = 49;
			dg2.getColumnAt(0).headerText = "任务名称";
			dg2.getColumnAt(1).headerText = "任务描述";
			dg2.getColumnAt(2).headerText = "任务奖励";
			dg2.getColumnAt(3).headerText = "进度";
			dg2.setSize(360,170);
			dg2.setStyle("cellRenderer",Resource.uicomponent.TaskCellClass);
			
			dg2.getColumnAt(0).width = 80;
			dg2.getColumnAt(1).width = 140;
			dg2.getColumnAt(2).width = 90;
			dg2.getColumnAt(3).width = 50;
			dg2.rowHeight = 70;
			
			
			addChild(dg2);
			
			
			
		}
		
		private function drawFrame(object:Object):void
		{
			shape.graphics.clear();
			with (shape.graphics)
			{
				lineStyle(2,0x999999);
				drawRoundRect(dg.x,dg.y +1,object.width,object.height ,15,15);
			}
		}
		
		private function drawLine():void
		{
			this.dg.graphics.clear();
			with(this.dg.graphics)
			{
				
				var lastWidth:int=0;
				var height:int = dg.headerHeight;
				for (var i:int=0;i<dg.getColumnCount() - 1; i++)
				{
					if (dg.getColumnAt(i).visible)
					{
						lastWidth += dg.getColumnAt(i).width;
						lineStyle(1,0x000000);
						moveTo(lastWidth - 1,height);
						lineTo(lastWidth - 1,dg.height);
						
						
						lineStyle(1,0x333333);
						moveTo(lastWidth,height);
						lineTo(lastWidth,dg.height);
					}
				}
			}	
			
		}
		
		private function drawLine2(arr:Array):void
		{
			this.dg2.graphics.clear();
			with(this.dg2.graphics)
			{
				
				var lastWidth:int=0;
				var height:int = dg2.headerHeight;
				for (var i:int=0;i<arr.length; i++)
				{
					
					lastWidth += arr[i];
					lineStyle(1,0x000000);
					moveTo(lastWidth - 1,height);
					lineTo(lastWidth - 1,dg2.height);
						
						
					lineStyle(1,0x333333);
					moveTo(lastWidth,height);
					lineTo(lastWidth,dg2.height);

				}
			}
		}
		
		private function initTab():void
		{
			tab = new Tab();
			tab.addTabBar(new TabBar(0,"日常任务",100),true);
			tab.addTabBar(new TabBar(1,"普通任务",100));
			tab.x = 40;
			tab.y = 20;
			addChild(tab);
			
			tab.addEventListener(TabEvent.TABBAR_CLICK,onTabBarClick);
		}
		
		private function onTabBarClick(event:TabEvent):void
		{
			(panelControl as TaskPanelControl).onTabSwitch(tab.selectedIndex);
			
		}
		
		public function update(dataProvider:*):void
		{
			
			clear();
			for (var i:int=0; i<dataProvider.length; i++)
			{
				var data:Object = dataProvider.getItemAt(i);
				if (data.state == "0")
				{
					data.icon = new ImgButton(0,0,i,onCell,60,22,"butBg3",null,"接受任务");
					data.state = "";
				
					data.icon.param1 = 0;
					buts.push(data.icon);
				}
				else if (data.state == "2")
				{
					data.icon = new ImgButton(0,0,i,onCell,60,22,"butBg",null,"交任务");
					data.state = "";
					buts.push(data.icon);
					data.icon.param1 = 2;
				}
			}
			
			dg.dataProvider = dataProvider; 
			dg2.dataProvider = dataProvider;
			
		}
		
		private function setButton():void
		{
			clear();
			if (dg.length > 0)
			{
				for (var i:int=0; i< dg.length; i++)
				{
					var dataCell:* = dg.getCellRendererAt(i,3);
					
					var imgButton:ImgButton;
					if ( dataCell)
					{
						switch (dataCell.listData.label)
						{
							case "0":
								imgButton = new ImgButton(0,0,i,onCell,60,22,"butBg3",null,"接受任务");
								imgButton.x = (dataCell.width - imgButton.width) * .5;
								imgButton.y = (dataCell.height - imgButton.height) * .5;
								buts.push(imgButton);
								//dataCell.addChild(imgButton);
								//dataCell.listData.icon = imgButton;
								dataCell.redrawLayout(imgButton);
								dataCell.mouseChildren = true;
								imgButton.param1 = 0;
								break;
							case "2":
								imgButton = new ImgButton(0,0,i,onCell,60,22,"butBg2",null,"接受完成");
								imgButton.param1 = 2;
								imgButton.x = (dataCell.width - imgButton.width) * .5;
								imgButton.y = (dataCell.height - imgButton.height) * .5;
								buts.push(imgButton);
								//dataCell.listData.icon = imgButton;
								dataCell.redrawLayout(imgButton);
								dataCell.mouseChildren = true;
								break;
						}
						
					}
					
					
				}
			}
			isUpdated = false;
			
			
		}
		
		private function clear():void
		{
			var but:ImgButton;
			for each (but in buts)
			{
				DisplayTool.removeChild(but.parent,but);
				but.destroy();
			}
			buts.length = 0;
		}

		
		private function onCell(event:MouseEvent):void
		{
	
			event.target.mouseEnabled = false;
			var index:int = event.target.id;
			var taskId:String = dg.getItemAt(index).id;
			
			if (event.target.param1 == 0)
			{
				(panelControl as TaskPanelControl).onTaskAccept(taskId);
			}
			else
			{
				(panelControl as TaskPanelControl).onTaskSubmit(taskId);
			}
		}
	}
}